﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;

namespace StudentWeb.Api.Controllers
{
    public class AdminsController : ApiController
    {
        AdminsBO _AdminsBO = new AdminsBO();
        #region 管理员信息 登录 
        [HttpPost]
        public IHttpActionResult Login([FromBody]JObject obj)
        {
            dynamic model = obj;
            string AdminUsername = model.AdminUsername ?? "";
            string AdminPassword = model.AdminPassword ?? "";
            if (string.IsNullOrEmpty(AdminUsername) || string.IsNullOrEmpty(AdminPassword))
            {
                return Json(new Result(ResultCode.Fail, "用户名或密码不能为空"));
            }
            try
            {
                var result = _AdminsBO.Login(AdminUsername, AdminPassword);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 管理员信息 注册 
        [HttpPost]
        public IHttpActionResult SaveAdminsInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            string AdminUsername = model.AdminUsername ?? "";
            string AdminPassword = model.AdminPassword ?? "";
            string AdminPhone = model.AdminPhone ?? "";
            string AdminRegistrationDate = model.AdminRegistrationDate ?? "";
            if (string.IsNullOrEmpty(AdminUsername))
            {
                return Json(new Result(ResultCode.Fail, "用户名不能为空"));
            }
            if (string.IsNullOrEmpty(AdminPassword))
            {
                return Json(new Result(ResultCode.Fail, "密码不能为空"));
            }
            if (string.IsNullOrEmpty(AdminPhone))
            {
                return Json(new Result(ResultCode.Fail, "手机号不能为空"));
            }
            if (string.IsNullOrEmpty(AdminRegistrationDate))
            {
                return Json(new Result(ResultCode.Fail, "注册日期不能为空"));
            }
            try
            {
                var result = _AdminsBO.SaveAdminsInfo(model);
                return result ? Json(new Result(ResultCode.Success, "注册成功")) :
               Json(new Result(ResultCode.Fail, "注册失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 管理员信息 全部记录
        [HttpGet]
        public IHttpActionResult GetAdminsList()
        {
            try
            {
                var result = new AdminsBO().GetAdminsList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 管理员信息 单条记录
        [HttpPost]
        public IHttpActionResult GetAdminInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int AdminID = model.AdminID ?? 0;
            if (AdminID == 0)
            {
                return Json(new Result(ResultCode.Fail, "ID 不能为空"));
            }
            try
            {
                var result = _AdminsBO.GetAdminInfo(AdminID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 管理员信息 添加
        [HttpPost]
        public IHttpActionResult InsertAdminsInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            string AdminUsername = model.AdminUsername ?? "";
            string AdminPassword = model.AdminPassword ?? "";
            string AdminPhone = model.AdminPhone ?? "";
            string AdminRegistrationDate = model.AdminRegistrationDate ?? "";
            if (string.IsNullOrEmpty(AdminUsername))
            {
                return Json(new Result(ResultCode.Fail, "用户名不能为空"));
            }
            if (string.IsNullOrEmpty(AdminPassword))
            {
                return Json(new Result(ResultCode.Fail, "密码不能为空"));
            }
            if (string.IsNullOrEmpty(AdminPhone))
            {
                return Json(new Result(ResultCode.Fail, "电话号码不能为空"));
            }
            if (string.IsNullOrEmpty(AdminRegistrationDate))
            {
                return Json(new Result(ResultCode.Fail, "注册日期不能为空"));
            }
            try
            {
                var result = _AdminsBO.InsertAdminsInfo(model);
                return result ? Json(new Result(ResultCode.Success, "添加成功")) :
                Json(new Result(ResultCode.Fail, "添加失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 管理员信息 修改
        [HttpPost]
        public IHttpActionResult UpdateAdminsInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            int AdminID = model.AdminID ?? 0;
            string AdminUsername = model.AdminUsername ?? "";
            string AdminPassword = model.AdminPassword ?? "";
            string AdminPhone = model.AdminPhone ?? "";
            string AdminRegistrationDate = model.AdminRegistrationDate ?? "";
            if (AdminID == 0)
            {
                return Json(new Result(ResultCode.Fail, "管理员ID不能为空"));
            }
            if (string.IsNullOrEmpty(AdminUsername))
            {
                return Json(new Result(ResultCode.Fail, "用户名不能为空"));
            }
            if (string.IsNullOrEmpty(AdminPassword))
            {
                return Json(new Result(ResultCode.Fail, "密码不能为空"));
            }
            if (string.IsNullOrEmpty(AdminPhone))
            {
                return Json(new Result(ResultCode.Fail, "电话号码不能为空"));
            }
            if (string.IsNullOrEmpty(AdminRegistrationDate))
            {
                return Json(new Result(ResultCode.Fail, "注册日期不能为空"));
            }
            try
            {
                var result = _AdminsBO.UpdateAdminsInfo(model);
                return result ? Json(new Result(ResultCode.Success, "修改成功")) :
                Json(new Result(ResultCode.Fail, "修改失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 管理员信息 删除
        [HttpPost]
        public IHttpActionResult DeleteAdiminsInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int AdminID = model.AdminID ?? 0;
            if (AdminID == 0)
            {
                return Json(new Result(ResultCode.Fail, "ID 不能为空"));
            }
            try
            {
                var result = _AdminsBO.DeleteAdiminsInfo(AdminID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 用户信息 分页列表
        [HttpPost]
        public IHttpActionResult PageAdminsList([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail,
                "参数错误"));
                var wc = _PageAdminsListWhereC(model.condidate);
                var result = _AdminsBO.PageAdminsList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _PageAdminsListWhereC(dynamic qc)
        {
            var wc = string.Empty;
            if (qc == null) return wc;
            string tmp = "";
            tmp = ConvertHelper.DynamicToString(qc.AdminUsername);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" AdminUsername like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.AdminPassword);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" AdminPassword like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.AdminPhone);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" AdminPhone like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.AdminRegistrationDate);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" AdminRegistrationDate like '%{tmp}%'"; }
            return wc;
        }
        #endregion
    }
}
